<template>
	<view>
		<nav-bar title="商品结算"></nav-bar>
		<van-contact-card type="edit" :tel="address.tel" :name="address.name" @click="goAddres" />
	</view>
	<van-card v-for="(c,ci) in payGoods" @click="goGoodsDetail(c._id)" :price="c.price" :desc="c.goods_desc"
		:title="c.name" :thumb="c.thumb" :origin-price="c.pre_price">
		<template #num>
			<view class="cart-num">
				<view class="cart-num-btn">{{c.num}}</view>
			</view>
		</template>
	</van-card>
	<view class="">
		{{total}}
	</view>
	<view class="pay-bth">
		<van-button type="warning" @click="QBPay">钱包支付</van-button>
		<van-button type="success">微信支付</van-button>
	</view>
</template>

<script>
	import Storage from '@/untils/storage.js'
	const User = uniCloud.importObject('user')
	const Order = uniCloud.importObject('order')
	export default {
		data() {
			return {
				payGoods: []
			}
		},
		computed: {
			address() {
				if (this.$store.state.address.defaultAddress) {
					this.$store.commit('storageDefaultAddress', Storage.get('setDefaultAddress'))
				}
				return this.$store.state.address.defaultAddress
			},
			total() {
				let total = 0;
				for (let i = 0; i < this.payGoods.length; i++) {
					let goods = this.payGoods[i];
					total += goods.price * goods.num
				}
				return total
			}
		},
		onLoad() {
			this.payGoods = Storage.get('payGoods')
		},
		methods: {
			goAddres() {
				uni.navigateTo({
					url: '/pages/user/address/list'
				})
			},
			QBPay() {
				let that = this
				User.money(Storage.userinfo._id).then(res => {
					if (res.money > this.total) {
						return res;
					} else {
						uni.showModal({
							title: "余额不足",
							content: `余额为${res.money/100}元`
						})
					}
				}).then(res => {
					uni.showModal({
						title: "确定支付",
						content: `余额为${res.moner/100}元，支付完还剩${(res.money-this.total)/100}元`,
						success(sres) {
							if (sres.confirm) {
								Order.QBPay(that.payGoods, Storage.userinfo._id, that.address,this.total).then(
									payRes => {
										if(payRes.success){
											Storage.set("payGoods",[])
											uni.redirectTo({
												url:"/pages/order/list"
											})
										}
									}
								)
							}
						}
					})
				})
			}
		}
	}
</script>

<style>

</style>
